using System.Collections.Generic;
using System.Xml.Serialization;
using Microsoft.Office.Server.Search.Administration;

namespace Carlsberg.SSPC.SearchScopes.BO
{
    [XmlRoot("SearchScope")]
    public class SearchScopeInfo
    {
        private string alternateResultsPage;
        private string description;
        private bool displayInAdminUI;
        private string name;

        [XmlAttribute]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [XmlAttribute]
        public string AlternateResultsPage
        {
            get { return alternateResultsPage; }
            set { alternateResultsPage = value; }
        }

        [XmlAttribute]
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        [XmlAttribute]
        public bool DisplayInAdminUI
        {
            get { return displayInAdminUI; }
            set { displayInAdminUI = value; }
        }

        [XmlElement]
        public List<Rule> Rules
        {
            get { return rules; }
            set { rules = value; }
        }


        //Skip display groups as they are site specific
        //private List<string> displayGroups;

        //[XmlElement]
        //public List<string> DisplayGroups
        //{
        //    get { return displayGroups; }
        //    set { displayGroups = value; }
        //}
        
        private List<Rule> rules;

        public SearchScopeInfo()
        {
        }

        public SearchScopeInfo( Scope scope)
        {
            alternateResultsPage = scope.AlternateResultsPage;
            description = scope.Description;
            displayInAdminUI = scope.DisplayInAdminUI;
            name = scope.Name;

            //Skip display groups as they are site specific
            //ArrayList tmp = new ArrayList(scope.ContainingDisplayGroups);
            //displayGroups = new List<string>( (string[])tmp.ToArray(typeof(string)) );

            Rules = new List<Rule>();
            foreach (ScopeRule rule in scope.Rules)
            {
                Rules.Add( new Rule( rule ) );
            }                        
        }

        public void SetScopeProperties(Scope scope, SearchContext searchContext)
        {
            scope.DisplayInAdminUI = displayInAdminUI;
            scope.AlternateResultsPage = alternateResultsPage;
            scope.Description = description;

            //scope.Name = name;

            //Delete existing scopes
            List<ScopeRule> scopeRules = new List<ScopeRule>();
            foreach( ScopeRule sr in scope.Rules )
            {
                scopeRules.Add(sr);
            }

            //Cannot delete directly on an enumeration over Scope.Rules (would modify enum), and 
            // apparently zero based indexing into the collection does not work properly)
            foreach (ScopeRule sr in scopeRules)
            {

                sr.Delete();
            }

            foreach (Rule rule in Rules)
            {
                rule.AddScopeRule(scope.Rules, searchContext);
            }
        }
    }
}
